package com.example.lotterysystem.controller;

import com.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.example.lotterysystem.common.exception.ControllerException;
import com.example.lotterysystem.common.pojo.CommonResult;
import com.example.lotterysystem.common.utils.JacksonUtil;
import com.example.lotterysystem.controller.param.UserMessageLoginParam;
import com.example.lotterysystem.controller.param.UserPasswordLoginParam;
import com.example.lotterysystem.controller.param.UserRegisterParam;
import com.example.lotterysystem.controller.result.BaseUserInfoResult;
import com.example.lotterysystem.controller.result.UserLoginResult;
import com.example.lotterysystem.controller.result.UserRegisterResult;
import com.example.lotterysystem.service.IUserService;
import com.example.lotterysystem.service.IVerificationCodeService;
import com.example.lotterysystem.service.dto.UserDTO;
import com.example.lotterysystem.service.dto.UserLoginDTO;
import com.example.lotterysystem.service.dto.UserRegisterDTO;
import com.example.lotterysystem.service.enums.UserIdentifyEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private IUserService userService;

    @Autowired
    private IVerificationCodeService verificationCodeService;

    /**
     * 用户注册
     * @param userRegisterParam
     * @return
     */
    @PostMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(
            @Validated @RequestBody UserRegisterParam userRegisterParam){
        //日志打印
        log.info("userRegister userRegisterParam: {}" , JacksonUtil.writeValueAsString(userRegisterParam));
        //调用service
        UserRegisterDTO userRegisterDTO = userService.register(userRegisterParam);
        return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }


    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        UserRegisterResult result = new UserRegisterResult();
        if(null == userRegisterDTO){
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        result.setUserId(userRegisterDTO.getUserId());
        return result;
    }

    /**
     * 发送验证码
     * @param phoneNumber
     * @return
     */
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber){
        //日志打印
        log.info("sendVerificationCode phoneNumber: {}" , phoneNumber);
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 密码登录
     * @param userPasswordLoginParam
     * @return
     */
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> userPasswordLogin(@RequestBody
                                                               @Validated UserPasswordLoginParam userPasswordLoginParam){
        log.info("userPasswordLogin userPasswordLoginParam : {}", JacksonUtil.writeValueAsString(userPasswordLoginParam));
        UserLoginDTO userLoginDTO = userService.login(userPasswordLoginParam);
        return CommonResult.success(coverToUserLoginResult(userLoginDTO));
    }

    private UserLoginResult coverToUserLoginResult(UserLoginDTO userLoginDTO) {

        if(null == userLoginDTO){
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult userLoginResult = new UserLoginResult();
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setIdentity(userLoginDTO.getIdentity().name());
        return userLoginResult;
    }

    /**
     * 短信验证码登录
     * @param userMessageLoginParam
     * @return
     */
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> userMessageLogin(@RequestBody
                                                              @Validated UserMessageLoginParam userMessageLoginParam){
        log.info("userMessageLogin userMessageLoginParam : {}", JacksonUtil.writeValueAsString(userMessageLoginParam));
        UserLoginDTO userLoginDTO = userService.login(userMessageLoginParam);
        return CommonResult.success(coverToUserLoginResult(userLoginDTO));
    }

    @RequestMapping("/base-user/find-list")
    public CommonResult<List<BaseUserInfoResult>> findBaseUserInfo(String identity) {
        log.info("findBaseUserInfo identity: {}", identity);
        List<UserDTO> userDTOList = userService.finUserInfo(UserIdentifyEnum.forName(identity));
        return CommonResult.success(convertToList(userDTOList));
    }

    private List<BaseUserInfoResult> convertToList(List<UserDTO> userDTOList) {
        if(CollectionUtils.isEmpty(userDTOList)){
            return Arrays.asList();
        }
        return userDTOList.stream()
                .map(userDTO -> {
                    BaseUserInfoResult result = new BaseUserInfoResult();
                    result.setUserId(userDTO.getUserId());
                    result.setUserName(userDTO.getUserName());
                    result.setIdentity(userDTO.getIdentity().name());
                    return result;
                }).collect(Collectors.toList());
    }
}
